package com.hz.ruoyiequipment.data.controller;

import com.hz.ruoyiequipment.data.domain.AgrEquipment;
import com.hz.ruoyiequipment.data.domain.AgrEquipmentQx;
import com.hz.ruoyiequipment.data.domain.vo.QxEquipment;
import com.hz.ruoyiequipment.data.domain.vo.QxTimeData;
import com.hz.ruoyiequipment.data.service.AgrEquipmentQxService;
import com.hz.ruoyiequipment.data.service.AgrEquipmentService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author: xqd
 * @CreateDate: 2025/03/06/10:52
 * @Description:
 */
@RestController
@RequestMapping("/environmental")
@CrossOrigin
public class AgrEquipmentQxController extends BaseController {
    @Autowired
    private AgrEquipmentQxService agrEquipmentQxService;
    @Autowired
    private AgrEquipmentService agrEquipmentService;

    @RequiresPermissions("data:environmental:list")
    @GetMapping("/list")
    public TableDataInfo getQxEquipment(QxEquipment qxEquipment){
        startPage();
        return getDataTable(agrEquipmentService.getQxEquipment(qxEquipment));
    }

    @RequiresPermissions("data:environmental:detail")
    @GetMapping("/detail")
    public AjaxResult getQxDetail(Long equipmentId){
        return agrEquipmentService.getQxDetail(equipmentId);
    }

    @RequiresPermissions("data:environmental:insert")
    @PostMapping("/insert")
    public AjaxResult insertQx(@RequestBody QxEquipment qxEquipment){
        try {
            AgrEquipment agrEquipment = new AgrEquipment();
            agrEquipment.setEquipmentCode(qxEquipment.getEquipmentCode());
            agrEquipment.setEquipmentName(qxEquipment.getEquipmentName());
            agrEquipment.setEquipmentBrand(qxEquipment.getEquipmentBrand());
            agrEquipment.setEquipmentType(qxEquipment.getEquipmentType());
            agrEquipment.setEquipmentImg(qxEquipment.getEquipmentImg());

            AgrEquipmentQx agrEquipmentQx = getAgrEquipmentQx(qxEquipment);
            agrEquipmentService.insertQx(agrEquipment, agrEquipmentQx);
            return AjaxResult.success("新增设备成功");
        } catch (Exception e) {
            return AjaxResult.error("新增设备失败");
        }
    }

    public static AgrEquipmentQx getAgrEquipmentQx(QxEquipment qxEquipment){
        AgrEquipmentQx agrEquipmentQx = new AgrEquipmentQx();
        agrEquipmentQx.setQxFlow(qxEquipment.getQxFlow());
        agrEquipmentQx.setQxMaterial(qxEquipment.getQxMaterial());
        agrEquipmentQx.setQxInterval(qxEquipment.getQxInterval());
        agrEquipmentQx.setQxCapacity(qxEquipment.getQxCapacity());
        agrEquipmentQx.setQxInterface(qxEquipment.getQxInterface());
        return agrEquipmentQx;
     }

     @RequiresPermissions("data:environmental:update")
     @PostMapping("/update")
     public AjaxResult updateAgrEquipmentQx(@RequestBody QxEquipment qxEquipment){
         return AjaxResult.success(agrEquipmentQxService.updateAgrEquipmentQx(qxEquipment));
     }

     @RequiresPermissions("data:environmental:delete")
     @PostMapping("/delete")
     public AjaxResult delAgrEquipmentQxByEquipmentId(Long equipmentId){
         return AjaxResult.success(agrEquipmentQxService.delAgrEquipmentQxByEquipmentId(equipmentId));
     }



     @RequiresPermissions("data:environmental:timedata")
     @GetMapping("/timedata")
     public AjaxResult getQxTimeData(Long equipmentId){
         return AjaxResult.success(agrEquipmentQxService.getQxTimeData(equipmentId));
     }

     @RequiresPermissions("data:environmental:timedatas")
     @GetMapping("/timedatas")
     public AjaxResult getQxTimeDatas(Long equipmentId){
         return AjaxResult.success(agrEquipmentQxService.getQxTimeDatas(equipmentId));
     }

     @RequiresPermissions("data:environmental:head")
     @GetMapping("/head")
     public AjaxResult getQxTimeDataHead(Long equipmentId){
         return AjaxResult.success(agrEquipmentQxService.getQxTimeDataHead(equipmentId));
     }

     @RequiresPermissions("data:environmental:insertTimeData")
     @PostMapping("/insertTimeData")
     public AjaxResult insertQxTimeData(@RequestBody QxTimeData  qxTimeData){
         return agrEquipmentQxService.insertQxTimeData(qxTimeData);
     }

}
